import { Dog } from './dog'
import * as Blockly from 'blockly/core';
import { registerFieldColour } from '@blockly/field-colour';
import { Colors } from './colors';
import { Dom } from './dom';
import { Lists } from './lists';
import { Funcs } from './funcs';
import { Browser } from './browser';
import { WebCanvas } from './canvas';
import { Variables } from './variables';
import { Loops } from './loops';
import { Logic } from './logic';
import { Numbers } from './numbers';
import { Texts } from './texts';
import { Medias } from './medias';
import { Runtims } from './runtimes';
import { DogEditor } from '@/utils/dog-editor';

export namespace CustomBlocks {
    export function init() {
        Logic.init()
        Loops.init()
        Variables.init()
        Lists.init()
        Numbers.init()
        Texts.init()
        Runtims.init()
        Dom.init()
        Colors.init()
        Dog.init()
        Funcs.init()
        Browser.init()
        Medias.init()
        WebCanvas.init()
        registerFieldColour()
    }

    export function getCoreBlocks() {
        const ar: Blockly.utils.toolbox.ToolboxItemInfo[] = []
        ar.push(...[
            Logic.getTools(),
            Loops.getTools(),
            Lists.getTools(),
            Numbers.getTools(),
            Texts.getTools(),
            Runtims.getTools(),
            Colors.getTools(),
            Browser.getTools(),
            Variables.getTools(),
            Dom.getTools(),
            WebCanvas.getTools(),
            Medias.getTools(),
            Dog.getTools(),
            Funcs.getTools(),
            ...DogEditor.getPluginTools()
        ])
        return ar
    }

    export async function setCustonLocale(locale: string) {
        try {
            const data = await import(`@/locales/${locale}.json`);
            for (const key in data.default) {
                Blockly.Msg[key] = data.default[key]
            }

        } catch (_error) {
            console.log('load locale error', locale)
        }

        return Blockly.Msg
    }
}